#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include "dominion/dominion.h"
#include "dominion/dominion_helpers.h"
#include "dominion/rngs.h"
#include "dominion/rngs.c"
#include "dominion/interface.h"

#define NUM_TESTS 50

int random_card() {
	int random = rand() % 16;
	switch(random)
	{
		case 0:
			return copper;
		case 1:
			return silver;
		case 2:
			return gold;
		case 3:
			return estate;
		case 4:
			return duchy;
		case 5:
			return province;
		case 6:
			return adventurer;
		case 7:
			return council_room;
		case 8:
			return feast;
		case 9:
			return gardens;
		case 10:
			return mine;
		case 11:
			return remodel;
		case 12:
			return smithy;
		case 13:
			return village;
		case 15:
			return baron;
		case 16:
			return great_hall;
		default:
			return 0;
	}
}

int main (int argc, char ** argv) {
	
	/*modified from the initialization in the provided testUpdateCoins.c file */
    int seed = 1000;
    int numPlayer = 2;
    int m[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
	int bonus = 0;
	int i, j, k, l, r;
	int test_count = 0; 
	int tests_passed = 0; 
	int tests_failed = 0; 	
	
	struct gameState test_one; 
	
	i = 0;
		for (j = 0; j < NUM_TESTS; j++) {
			for (k = 0; k < 10; k++) {
				memset(&test_one, 23, sizeof(struct gameState));   // clear the game state
				r = initializeGame(numPlayer, m, seed, &test_one); // initialize a new game
				// make sure there's an adventurer in each hand! always set to 1st position
				if (k == 0) {
					test_one.deck[i][k] = adventurer;
				//otherwise, the rest of the 10 cards are random
				} else {
					test_one.deck[i][k] = random_card();
				}
				//once we're on the last loop, we test
				if (k == 9) {
					//set hand cards after deck cards are set -- the first 5 random cards
					for (l = 0; l < 5; l++) {
						test_one.hand[i][l] = test_one.deck[i][l];
					}
					//set handCount, whoseTurn, and coins before testing
					test_one.handCount[i] = 5;
					test_one.whoseTurn = i;
					test_one.coins = 0;
					
					//set random value for deckCount to get adequate coverage for when the deckcount is low
					test_one.deckCount[i] = rand() % 10;
					
					cardEffect(adventurer, 0, 0, 0, &test_one, 0, &bonus);
					//test handCount being 2 higher
					if (test_one.handCount[i] == 7) {
						test_count++;
						tests_passed++;
						printf("Adventurer random test: SUCCESS, handCount is 2 higher. %i tests ran, %i tests successful, %i tests failed. \n", test_count, tests_passed, tests_failed);
					}
					else {
						test_count++;
						tests_failed++;
						printf("Adventurer random test: FAIL, handCount is not 2 higher. %i tests ran, %i tests successful, %i tests failed. \n", test_count, tests_passed, tests_failed);
					}
					//test coin count being higher
					if (test_one.coins > 0) {
						test_count++;
						tests_passed++;
						printf("Adventurer random test: SUCCESS, coin amount increased. %i tests ran, %i tests successful, %i tests failed. \n", test_count, tests_passed, tests_failed);
					}
					else {
						test_count++;
						tests_failed++;
						printf("Adventurer random test: FAIL, coin amount has not increased. %i tests ran, %i tests successful, %i tests failed. \n", test_count, tests_passed, tests_failed);
					}
				}
			}
		}
	
	return 0;
}